package com.wswy.wzcx.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Keep;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.wswy.wzcx.R;
import com.wswy.wzcx.ui.persenter.JZCFPresenter;
import com.wswy.wzcx.ui.view.JZCFView;
import com.wswy.wzcx.widget.EventMessage;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by wangduheng26 on 2018/3/6.
 * wangduheng26@gmail.com
 */

public class JZCFFragment extends ILoginFragment implements JZCFView {
    public static final String TAG = "JZCFFragment";
    FrameLayout frameLayout;
    JZCFPresenter presenter;
    private String messageCode = "";

    public static JZCFFragment newInstance() {
        Bundle args = new Bundle();
        JZCFFragment fragment = new JZCFFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        presenter.onCreateOptionsMenu(menu, inflater);
//        menu.add(getString(R.string.edit))
//                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        super.onCreateOptionsMenu(menu, inflater);


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Context context = getContext();
        assert context != null;
        frameLayout = new FrameLayout(context);
        presenter = new JZCFPresenter(context, this);
        presenter.onCreate(frameLayout);
        return frameLayout;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == R.id.action_edit) {
            presenter.setShowEditMode();
            return true;
        } else if (itemId == R.id.action_edit_complete) {
            presenter.exitEdit();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.onResume();
    }

    @Override
    public void onStart() {
        super.onStart();
        presenter.onStart();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter.onActivityCreated(savedInstanceState);
    }

    @Override
    public void setupPage() {
        presenter.setupPage(messageCode);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }

    @Override
    public String getFragmentTitle() {
        return "驾驶证查分";
    }


    @Keep
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventMessage(EventMessage message) {
        EventBus.getDefault().removeStickyEvent(message);
        messageCode = message.getMessage();
    }

    @Override
    public Activity activity() {
        return getActivity();
    }
}
